package com.sftz.modules.minapp.homework.module;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import com.sftz.assets.utils.QuestionUtil;
import com.sftz.framework.rersult.MobileResult;
import com.sftz.modules.minapp.homework.form.HomeworkAnswerDetailForm;
import com.sftz.modules.minapp.homework.form.HomeworkAnswerFileForm;
import com.sftz.modules.minapp.homework.form.HomeworkCommitForm;
import com.sftz.modules.minapp.homework.form.HomeworkDeleteAnswerFileForm;
import com.sftz.modules.minapp.homework.form.HomeworkEntryForm;
import com.sftz.modules.minapp.homework.form.HomeworkResultForm;
import com.sftz.modules.minapp.homework.form.HomeworkSaveQuestAnswerForm;
import com.sftz.modules.minapp.homework.form.UpdateElapsedTimeForm;
import com.sftz.modules.minapp.homework.service.HomeworkViewService;

@IocBean
@At("/minapp/homework")
public class HomeworkModule {

	@Inject
	private HomeworkViewService homeworkViewService;

	/**
	 * 作业入口页
	 */
	@At
	@Ok("json")
	public Object entry(@Param("..") final HomeworkEntryForm form) {
		try {
			return homeworkViewService.entry(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 查看作答记录接口
	 */
	@At
	public Object answerRecord(@Param("..") final HomeworkEntryForm form) {

		try {
			return homeworkViewService.allRecord(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 查看某条作答记录的详情页
	 */
	@At
	public Object answerDetail(@Param("..") final HomeworkAnswerDetailForm form) {

		try {
			return homeworkViewService.answerDetail(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 答题页
	 */
	@At
	public Object answer(@Param("..") final HomeworkEntryForm form) {
		try {
			return homeworkViewService.answer(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 作答结果页
	 */
	@At
	public Object result(@Param("..") final HomeworkResultForm form) {
		try {
			return homeworkViewService.result(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 保存试题作答信息
	 */
	@At
	public Object saveQuestionAnswer(@Param("..") final HomeworkSaveQuestAnswerForm form) {
		try {
			return homeworkViewService.saveQuestionAnswer(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 更新作答时间
	 */
	@At
	public Object updateElapsedTime(@Param("..") final UpdateElapsedTimeForm form) {
		try {
			return homeworkViewService.updateElapsedTime(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 提交作业接口
	 */
	@AdaptBy(type = JsonAdaptor.class)
	@At
	public Object commitHomework(@Param("..") final HomeworkCommitForm form) {
		try {
			return homeworkViewService.commitHomework(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 	材料题视频上传
	 */
	@At
	@Ok("json")
	@AdaptBy(type = UploadAdaptor.class, args = { "ioc:tmpUpload" })
	public Object uploadVideo(final @Param("file") TempFile file, @Param("..") final HomeworkAnswerFileForm form) {
		try {
			return homeworkViewService.uploadFile(file, form, QuestionUtil.UPLOAD_TYPE_VIDEO);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 	材料题图片附件上传
	 */
	@At
	@Ok("json")
	@AdaptBy(type = UploadAdaptor.class, args = { "ioc:imgUpload" })
	public Object uploadFile(final @Param("file") TempFile file, @Param("..") final HomeworkAnswerFileForm form) {
		try {
			return homeworkViewService.uploadFile(file, form, QuestionUtil.UPLOAD_TYPE_IMG);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

	/**
	 * 删除材料题答案附件
	 */
	@At
	public Object deleteFile(@Param("..") final HomeworkDeleteAnswerFileForm form) {
		try {
			return homeworkViewService.deleteFile(form);
		} catch (Exception e) {
			e.printStackTrace();
			return MobileResult.error("接口调用失败", null);
		}
	}

}